<%-- 
    Document   : trade.futures.transactions.ajax-source
    Created on : Dec 14, 2012, 11:41:05 AM
    Author     : kiendn
--%>

<%@page import="pc.util.Utils"%>
<%@page import="wstrader.task.trade.futures.Transactions"%>
<%@page import="wstrader.Trader"%>
<%@page import="wstrader.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%
            Users usr = Trader.getUser(request.getParameter("uid"));
            Transactions task = usr.trade.futures.transactions;

            String cnt_dt = request.getParameter("cnt_dt");
            String BotSold = request.getParameter("bot_sold");
            String TerMrk = request.getParameter("ter_mrk");
            String TerMth = request.getParameter("ter_mth");//Utils.strToDate(getRequest().getStringValue("ter_mth"));
            int BrokerId = Integer.parseInt(request.getParameter("broker"));
            if (TerMrk.compareTo("") == 0) {
                TerMrk = "0";
            }
            if (BotSold.compareTo("") == 0) {
                BotSold = "0";
            }
            String Sql = "Select *,sum(futures_close_out.lot_closed) AS cllot from futures_contract "
                    + "LEFT OUTER JOIN futures_close_out ON (futures_contract.id=futures_close_out.transaction_id) "
                    + "where (bot_sold = '" + BotSold + "' OR " + BotSold.equals("0") + ") "
                    + "and (terminal_market = '" + TerMrk + "' OR " + TerMrk.equals("0") + ") "
                    + "and (broker_id = " + BrokerId + " OR " + (BrokerId == 0) + ") "
                    + "and (terminal_month = '" + TerMth + "' OR " + TerMth.equals("1970-01-01") + " OR (terminal_month IS NULL)) "
                    + "and (futures_contract.transaction_date = '" + cnt_dt + "' OR " + cnt_dt.equals("1970-01-01") + ") "
                    + "GROUP BY futures_contract.id,futures_contract.ref_number, "
                    + "futures_contract.transaction_date,futures_contract.bot_sold, "
                    + "futures_contract.tons,futures_contract.lots,futures_contract.hedge_price, "
                    + "futures_contract.differential,futures_contract.price,futures_contract.fixation_id, "
                    + "futures_contract.terminal_month,futures_contract.terminal_market, "
                    + "futures_contract.quality_id,futures_contract.broker_id,futures_contract.active order by futures_contract.id desc";
            java.sql.ResultSet Rs = task.getSQLCommand().executeQuery(Sql);
        %>
        <link href="styles/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
        <link href="styles/demo_table_jui.css" rel="stylesheet" type="text/css"/>
        <style type="text/css">

            .style3 {
                font-family: Arial;
                font-size: 12px;
            }

        </style>
    </head>
    <body>
        <input name="sel_id" type="hidden" id="sel_id" value="">
        <input name="tot_cl_bot" type="hidden" id="tot_cl_bot" value="0">
        <input name="tot_cl_sold" type="hidden" id="tot_cl_sold" value="0">
        <input name="futures_contract" type="hidden" id="futures_contract" value="0">
        <table id="mytable" style="width: 100% !important">
            <thead>
                <tr>
                    <th class="style11">Trn.Ref.No</th>
                    <th class="style11">Trn.Date</th>
                    <th class="style11">Lots</th>
                    <th class="style11">Bot/Sold</th>
                    <th class="style11">Ter.Mth</th>
                    <th class="style11">Price</th>
                    <th class="style11">Close Out</th>
                </tr>
            </thead>
            <tbody>
                <%
                    int dLoop = 0;
                    Rs.beforeFirst();
                    int n = 1;
                    int i = 0;
                    while (Rs.next()) {
                        i = Rs.getInt("id");
                        float cllot = 0;

                        cllot = Rs.getFloat("cllot");
                        if ((Rs.getFloat("lots") - cllot) > 0) {
                %>
                <tr id="<%=Rs.getString("bot_sold") + "-" + Rs.getInt("id")%>" onDblClick="cardViewClick(this);" onClick="highlightOn(this);">          
                    <td width="17%" align="center"><span class="style3"><%=Rs.getString("ref_number")%></span></td>
                    <td width="12%" align="center"><span class="style3"><%=Utils.dateToStr(Rs.getDate("transaction_date"))%></span></td>
                    <td width="18%" align="right"><span class="style3"><label id = "lot_id_<%=n%>"><%=Utils.numberToStr(Rs.getFloat("lots"), 0)%></label></span></td>
                    <td width="13%" align="center"><span class="style3"><%=Rs.getString("bot_sold")%>
                            <input type="hidden" id="type_id_<%=n%>" name="type_id_<%=n%>" value ="<%=Rs.getString("bot_sold")%>">
                        </span></td>
                        <%
                            String Ter_dt = Utils.dateToStr(Rs.getDate("terminal_month"));
                            if (Ter_dt.compareTo("") == 0) {
                                Ter_dt = "";
                            } else {
                                Ter_dt = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
                            }
                        %>
                    <td width="13%" align="center"><span class="style3"><%=Ter_dt%></span></td>
                    <td width="14%" align="center"><span class="style3"><%=Utils.numberToStr(Rs.getFloat("price"), 2)%></span></td>
                    <td width="7%" align="right">
                        <input type="checkbox" id = "cb_<%=n%>" name="cb_<%=n%>" onClick="doOptCheck('cb_ + <%=i%>')">
                        <input type="hidden" id="inv_id_<%=n%>" value="<%=Rs.getInt("id")%>"/>
                    </td>

                </tr>
                <%
                            n = n + 1;
                        }
                        dLoop = dLoop + 1;
                    }
                %>
            </tbody>
            <tfoot>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                </tr>
            </tfoot>
        </table>
            <input type="hidden" id="dLoop" value="<%=dLoop%>"/>
        <script type="text/javascript">
            $(document).ready(function(){
                oTable = $("#mytable").dataTable({
                    "sScrollY": 250,
                    "bJQueryUI": true,
                    "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
                    "sPaginationType": "full_numbers"
                });
            });
            
            function doOptCheck(chb)
            {
                var form = document.formMain;
                var dLoop = $("#dLoop").val();
                var cl_lot =0;
                var type ="";
                var b = 0;
                var s = 0;
                form.sel_id.value ="";
                for (var i = 1;i<dLoop; i++)
                {
                    var inv_id = $("#cb_" + i);
                    if (inv_id == null) break;
                    if (inv_id != null) cl_lot = $("#cb_" + i).val()*1; 
		
                    if ($("#cb_" + i).is(":checked")) 
                    {
                        var v = $("#inv_id_" + i).val();
                        var l = 0;
                        l = $("#lot_id_" + i).text();
                        type = $("#type_id_" + i).val();
			
                        if (type == 'B') b = (b*1) + (l*1);
                        if (type == 'S') s = (s*1) + (l*1);                        
                        
                        if ($("#sel_id").val() == "")
                        {
                            $("#sel_id").attr("value",v);
                        }
                        else
                        {
                            $("#sel_id").attr("value",$("#sel_id").val() + "," + v);                            
                        }
                    }
		
                }
                $("#tot_cl_bot").attr("value",b);
                $("#tot_cl_sold").attr("value",s);               
            }
        </script>
    </body>
</html>
